<template>
    <div>
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="query.keyWord" placeholder="学员姓名/考试名称" clearable></el-input>
        </el-form-item>

        <el-form-item>
          <el-select clearable filterable v-model="query.gradeType" placeholder="请选择年级">
            <el-option
              v-for="item in gradeTypeList"
              :key="item.id"
              :label="item.value"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-select
             v-if="query.gradeType"
             clearable filterable
             v-model="query.subjectId"
             placeholder="请选择科目">
            <el-option
              v-for="item in subjectList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button
            v-if="hasPermission(['system:exam:list'])"
            @click="searchExamInfo"
            icon="el-icon-search" type="primary">查询</el-button>
        </el-form-item>
      </el-form>

      <el-table
        :data="examInfoList"
        border
        v-loading="loading"
        style="width: 100%;">

        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50">
        </el-table-column>

        <el-table-column
          prop="paper_name"
          align="center"
          min-width="200"
          label="考试名称">
        </el-table-column>

        <el-table-column
          prop="student_name"
          align="center"
          label="学员姓名">
        </el-table-column>

        <el-table-column
          prop="sex"
          align="center"
          label="性别">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.sex === 1" size="small">男</el-tag>
            <el-tag v-else size="small">女</el-tag>
          </template>
        </el-table-column>

        <el-table-column
          prop="grade_type"
          align="center"
          label="所属年级">
          <template slot-scope="scope">
            <el-tag size="small">{{scope.row.grade_type | getGradeName}}</el-tag>
          </template>
        </el-table-column>

        <el-table-column
          prop="subject_name"
          align="center"
          label="考试科目">
        </el-table-column>

        <el-table-column
          prop="create_date"
          align="center"
          :formatter="formatterCreateDate"
          label="考试时间">
        </el-table-column>

        <el-table-column
          prop="mark"
          align="center"
          label="考试分数">
          <template slot-scope="scope">
            <el-tag v-if="!scope.row.correct_flag" type="danger">未评分</el-tag>
            <el-tag v-else>{{scope.row.mark}}</el-tag>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        background
        @size-change="changeSize"
        @current-change="changePage"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        :total="totalCount"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>
</template>

<script>
    import { getGradeTypeListApi } from '@/utils/system-dict-api'
    let that = null
    export default {
      name: 'exam',
      data () {
        return {
          query: {
            gradeType: '',
            keyWord: ''
          },
          subjectList: [],
          examInfoList: [],
          loading: true,
          gradeTypeList: [],
          currentPage: 1,
          keyWord: '',
          pageSize: 10,
          totalCount: 0
        }
      },

      beforeCreate () {
        that = this
      },

      watch: {
        'query.gradeType' (val) {
          let that = this
          that.axios.get(that.$http.httpUrl('/system/subject'), {
            params: {
              gradeType: val
            }
          }).then(function (response) {
            that.subjectList = response.data.data.dataList
          }).catch(function (error) {
            console.log(error)
          })
        }
      },

      mounted () {
        that.getExamInfoList()
        getGradeTypeListApi(function (data) {
          that.gradeTypeList = data
        })
      },

      methods: {
        formatterCreateDate (row, column) {
          if (!row.create_date) {
            return '--'
          }
          return that.moment(row.create_date).format('YYYY-MM-DD HH:mm')
        },

        searchExamInfo () {
          that.currentPage = 1
          that.getExamInfoList()
        },

        changeSize (val) {
          that.pageSize = val
          that.currentPage = 1
          that.getExamInfoList()
        },

        changePage (val) {
          that.currentPage = val
          that.getExamInfoList()
        },

        getExamInfoList () {
          that.axios.get(that.$http.httpUrl('/system/exam'), {
            params: {
              keyWord: that.query.keyWord,
              subjectId: that.query.subjectId,
              gradeType: that.query.gradeType,
              offset: (that.currentPage - 1) * that.pageSize,
              limit: that.pageSize
            }
          }).then(function (response) {
            that.examInfoList = response.data.data.dataList
            that.totalCount = response.data.data.total
            that.loading = false
          })
        }
      },

      filters: {
        getGradeName (gradeType) {
          for (let i in that.gradeTypeList) {
            if (that.gradeTypeList[i].code === gradeType) {
              return that.gradeTypeList[i].value
            }
          }
        }
      }
    }
</script>

<style scoped>

</style>
